package edu.stu.book.book.keeping.component

import android.content.Context
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PhotoAlbum
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.key
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberImagePainter
import edu.stu.book.book.keeping.R
import edu.stu.book.book.keeping.db.dao.Cost
import edu.stu.book.book.keeping.utils.ColorUtils
import edu.stu.book.book.keeping.utils.CostUtils

@Composable
fun CostItem(
    context: Context,
    cost: Cost,
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {}
) {

    Row(
        modifier = modifier
            .height(40.dp)
            .fillMaxWidth()
            .clickable(
                indication = null,
                interactionSource = remember { MutableInteractionSource() }
            ) {
                onClick.invoke()
            },
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Row(
            horizontalArrangement = Arrangement.Start,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                modifier = Modifier.size(24.dp),
                imageVector = ImageVector.vectorResource(CostUtils.getCostTypeIcon(cost.typeId)),
                contentDescription = null,
                tint = ColorUtils.getColor(context)
            )

            Spacer(Modifier.padding(end = 10.dp))

            val text = remember(cost.description, cost.typeId) {
                cost.description.ifEmpty {
                    CostUtils.getCostTypeText(cost.typeId)
                }
            }

            Text(text = text, fontSize = 17.sp)
        }

        val moneyText = if (cost.type == 0) {
            "-${cost.money}"
        } else {
            "${cost.money}"
        }

        Text(text = moneyText)
    }
}
